﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml.Linq;
using System.Runtime.InteropServices;
using WoW.Armory.Data;
using System.Xml.Serialization;
using System.Xml;

namespace WoW.Armory
{
    /// <summary>
    /// World of Warcraft - Armory Class
    /// Class to export armory via COM objects to javascript.
    /// </summary>
    [ComVisible(true)]
    [XmlRoot("page")]
    public class Armory : IDisposable
    {
        #region Enums
        /// <summary>
        /// Armory Regions
        /// </summary>
        public enum Regions
        { 
            US,
            EU,
            KR
        }
        #endregion

        #region Fields
        private string _realm;
        private Regions _region;
        private string _characterName;
        private CharacterInfo _characterInfo;
        private string _lang;
        #endregion

        #region Properties
        [XmlAttribute("lang")]
        public string Lang
        {
            get { return _lang; }
            set { _lang = value; }
        }

        /// <summary>
        /// Return character info object
        /// </summary>
        [XmlElementAttribute("characterInfo")]
        public CharacterInfo CharacterInfo
        {
            get { return _characterInfo; }
            set { _characterInfo = value; }
        }

        /// <summary>
        /// Return Armory URL
        /// </summary>
        [XmlIgnore()]
        public string URL
        {
            get
            {
                string url = string.Empty;
                switch (_region)
                {
                    case Regions.US:
                        url = "http://www.wowarmory.com/";
                        break;
                    case Regions.EU:
                        url = "http://eu.wowarmory.com/";
                        break;
                    case Regions.KR:
                        url = "http://kr.wowarmory.com/";
                        break;
                }
                return url;
            }
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Represent a empry armory object. 
        /// </summary>
        public Armory()
        {
        
        }

        /// <summary>
        /// Represent a empty  Armory object with parameters.
        /// </summary>
        /// <param name="region">Armory site region.</param>
        /// <param name="realm">Character Realm.</param>
        /// <param name="characterName">Character Name.</param>
        public Armory(string region, string realm, string characterName)
        {
            try
            {    
                _realm = realm;
                _region = (Regions)Enum.Parse(typeof(Regions), region);
                _characterName = characterName;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region eMethods
        /// <summary>
        /// Load the armory data.
        /// </summary>
        /// <returns>Return a loaded armory object.</returns>
        public Armory Load()
        {
            try
            {
                XmlSerializer deserializer = new XmlSerializer(typeof(Armory));
                return (Armory)deserializer.Deserialize(GetXML("character-sheet.xml"));
            }
            catch (Exception)
            {
                throw;
            }
        }

        private XmlTextReader GetXML(string fileName)
        {
            WebClient wc = new WebClient();
            wc.BaseAddress = this.URL;
            wc.QueryString.Add("r", this._realm);
            wc.QueryString.Add("n", this._characterName);
            wc.Headers.Add("user-agent", "MSIE 7.0");
            wc.Credentials = CredentialCache.DefaultCredentials;
            return new XmlTextReader(wc.OpenRead(fileName));
        }
        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            //Do Nothing at this Time
            //Future Implementation
        }

        #endregion
    }
}
